<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:x="https://www.texmacs.org/2002/extensions" xmlns:m="http://www.w3.org/1998/Math/MathML">
  <head>
    <title>TeXmacs notes</title>
    <meta content="TeXmacs 1.99.15" name="generator"></meta>
    <link href="../resources/notes-base.css" type="text/css" rel="stylesheet"></link>
    <link href="../resources/blog-icon.png" rel="icon"></link>
    <script src="../resources/highlight.pack.js" language="javascript" defer></script>
    <script src="../resources/notes-base.js" language="javascript" defer></script>
  </head>
  <body>
    <div class="toggle" style="display: none">
      <p>
        
      </p>
    </div>
    <div class="notes-header">
      <p>
        <img class="image" src="../resources/texmacs-blog-transparent.png" width="28.116784"></img><span style="margin-left: 2pt"></span><a href="./main.html">[main]</a><em
        class="notes-header-name">Notes on TeXmacs</em>
      </p>
    </div>
    <p>
      <a id="auto-1"></a>
    </p>
    <h1>Example of TikZ figure embedding within a document<span style="margin-left: 1em"></span></h1>
    <p>
      Besides <a href="./a-tikz-example.html">generating Ti<em>k</em>Z figures within a <class style="font-variant: small-caps">Graph</class>
      plugin session</a>, it is possible to embed them directly in a document,
      so that they are displayed as an image (without the code and the plugin
      prompt), while keeping them editable.
    </p>
    <p>
      See <class class="tmweb-menu" style="font-family: sans-serif">Help</class>&rarr;<class class="tmweb-menu" style="font-family: sans-serif">Manual</class>&rarr;<class
      class="tmweb-menu" style="font-family: sans-serif">Use TeXmacs as an interface</class><a id="auto-2"></a> and its
      Section 4: <strong>Plug-ins as scripting language</strong> for a general
      description of the facility.
    </p>
    <p>
      Let us see here how to use the <class style="font-variant: small-caps">Fold</class> environment to
      embed an editable Ti<em>k</em>Z picture in a TeXmacs document, making in
      this way Ti<em>k</em>Z a flexible graphing tool for TeXmacs.
    </p>
    <p>
      The first step is inserting an executable field using the menu:
    </p>
    <p>
      <class class="tmweb-menu" style="font-family: sans-serif">Insert</class>&rarr;<class class="tmweb-menu" style="font-family: sans-serif">Fold</class>&rarr;<class
      class="tmweb-menu" style="font-family: sans-serif">Executable</class>&rarr;<class class="tmweb-menu" style="font-family: sans-serif">Graph</class><a
      id="auto-3"></a>
    </p>
    <p>
      (there are more options under <class class="tmweb-menu" style="font-family: sans-serif">Insert</class>&rarr;<class
      class="tmweb-menu" style="font-family: sans-serif">Fold</class>&rarr;<class class="tmweb-menu" style="font-family: sans-serif">Executable</class><a
      id="auto-4"></a>, but <class style="font-variant: small-caps">Graph</class> is the most recent plugin with
      the most flexible functionality).
    </p>
    <p>
      This results in a yellow-colored field, introduced by the
      &ldquo;<samp>Graph</samp>&rdquo; title on a gray background:
    </p>
    <p>
      <font color="grey"><table style="display: inline-table; vertical-align: middle">
        <tbody><tr>
          <td style="border-left: 0.5px solid; border-right: 0.5px solid; border-bottom: 0.5px solid; border-top: 0.5px solid; background-color: #8080A0; padding-left: 0.15em; padding-right: 0.15em"><font style="font-size: 90.0%"><class style="font-family: sans-serif"><font color="white">Graph</font></class></font></td>
          <td style="border-left: 0.5px solid; border-right: 0.5px solid; border-bottom: 0.5px solid; border-top: 0.5px solid; border-left: 0.5px solid; border-right: 0.5px solid; border-bottom: 0.5px solid; border-top: 0.5px solid; background-color: #ffff0020"><font color="black"><tt class="verbatim"></tt></font></td>
        </tr></tbody>
      </table></font>
    </p>
    <p>
      Press <font class="tmweb-key">&#x21E7;Return</font> to start typing or pasting
      code; pressing <font class="tmweb-key">Return</font> now would immediately compile
      the yet-empty code, yielding a blank figure. Once <font class="tmweb-key">&#x21E7;Return</font>
      has been pressed for the first time and it becomes possible to type
      code, the effects of the keys change: <font class="tmweb-key">&#x21E7;Return</font>
      itself executes the code (generates the image), while <font class="tmweb-key">Return</font>
      inserts a new line (the behaviour of the <font class="tmweb-key">Return</font> and
      <font class="tmweb-key">&#x21E7;Return</font> key combinations is described in the
      Manual at <class class="tmweb-menu" style="font-family: sans-serif">Help</class>&rarr;<class class="tmweb-menu" style="font-family: sans-serif">Manual</class>&rarr;<class
      class="tmweb-menu" style="font-family: sans-serif">Use TeXmacs as an interface</class><a id="auto-5"></a> in its
      Section 3).
    </p>
    <p>
      As a first example, let us choose one based on Ti<em>k</em>Z only, that
      does not need any further LaTeX packages, taking it from the guide
      &ldquo;A very minimal introduction to Ti<em>k</em>Z&rdquo; by Jacques
      Cr&eacute;mer (available at <a href="https://cremeronline.com/LaTeX/minimaltikz.pdf"><tt>https://cremeronline.com/LaTeX/minimaltikz.pdf</tt></a>,
      see section 4.1). Since we want to use the Ti<em>k</em>Z option of the
      <class style="font-variant: small-caps">Graph</class> plugin, the first line in our code has to
      consist of the characters <tt class="verbatim">%tikz</tt> :
    </p>
    <p>
      <font color="grey"><table style="display: inline-table; vertical-align: middle">
        <tbody><tr>
          <td style="border-left: 0.5px solid; border-right: 0.5px solid; border-bottom: 0.5px solid; border-top: 0.5px solid; background-color: #8080A0; padding-bottom: 0.15em; padding-top: 0.15em; text-align: center"><font style="font-size: 90.0%"><class style="font-family: sans-serif"><font color="white">Graph</font></class></font></td>
        </tr><tr>
          <td style="border-left: 0.5px solid; border-right: 0.5px solid; border-bottom: 0.5px solid; border-top: 0.5px solid; background-color: #ffff0020; width: 100%; padding-top: 0.3em; padding-bottom: 0.3em"><font color="black"><div class="compact-block">
            <pre class="verbatim" xml:space="preserve">
%tikz
\begin{tikzpicture}
\draw [fill=red,ultra thick] (0,0) rectangle (1,1);
\draw [fill=red,ultra thick,red] (2,0) rectangle (3,1);
\draw [blue, fill=blue] (4,0) -- (5,1) -- (4.75,0.15) -- (4,0);
\draw [fill] (7,0.5) circle [radius=0.1];
\draw [fill=orange] (9,0) rectangle (11,1);
\draw [fill=white] (9.25,0.25) rectangle (10,1.5);
\end{tikzpicture}</pre>
          </div></font></td>
        </tr></tbody>
      </table></font>
    </p>
    <p>
      
    </p>
    <p>
      Press <font class="tmweb-key">&#x21E7;Return</font> to execute the code and obtain
      after a few seconds the Ti<em>k</em>Z picture:
    </p>
    <p>
      <img src="embedding-tikz-figures-short-2.png" style="margin-left: -0.0206868686868687em; margin-bottom: -0.0206868686868687em; margin-right: -0.0206868686868695em; margin-top: 0em; vertical-align: 0em; height: 3.20464646464646em"></img>
    </p>
    <p>
      The appearance within the TeXmacs editor is a bit rough, but it is fine
      in the converted pdf documents.
    </p>
    <p>
      The executable <class style="font-variant: small-caps">Fold</class> environment with the
      Ti<em>k</em>Z picture can be placed inside a TeXmacs image environment:
    </p>
    <div style="margin-top: 1em; margin-bottom: 1em">
      <table style="width: 100%">
        <tbody><tr>
          <td style="text-align: center; padding-left: 0em; padding-right: 0em"><p>
            <img src="embedding-tikz-figures-short-4.png" style="margin-left: -0.0206868686868687em; margin-bottom: -0.0206868686868687em; margin-right: -0.0206868686868695em; margin-top: 0em; vertical-align: 0em; height: 3.20464646464646em"></img>
          </p></td>
        </tr><tr>
          <td style="text-align: center; padding-left: 0em; padding-right: 0em; height: 0.5em"></td>
        </tr><tr>
          <td style="text-align: center; padding-left: 0em; padding-right: 0em; padding-left: 1.5em; padding-right: 1.5em"><div class="caption">
            <font style="font-size: 90.0%"><p>
              <b>Figure 1. </b><a id="auto-6"></a>Ti<em>k</em>Z example from  Jacques
              Cr&eacute;mer's &ldquo;A very minimal introduction to
              Ti<em>k</em>Z&rdquo;
            </p></font>
          </div></td>
        </tr></tbody>
      </table>
    </div>
    <p>
      Placing the cursor at the generated figure (just after it: the cursor
      becomes as tall as the figure) makes two functions available.
    </p>
    <p>
      <em>First function: get back to the executable field</em>
    </p>
    <p>
      With the cursor at the figure (in this case, it can be placed before the
      figure as well as after it), press <font class="tmweb-key">Return</font> and obtain
      back the executable field, with the code ready for editing:
    </p>
    <p>
      <font color="grey"><table style="display: inline-table; vertical-align: middle">
        <tbody><tr>
          <td style="border-left: 0.5px solid; border-right: 0.5px solid; border-bottom: 0.5px solid; border-top: 0.5px solid; background-color: #8080A0; padding-bottom: 0.15em; padding-top: 0.15em; text-align: center"><font style="font-size: 90.0%"><class style="font-family: sans-serif"><font color="white">Graph</font></class></font></td>
        </tr><tr>
          <td style="border-left: 0.5px solid; border-right: 0.5px solid; border-bottom: 0.5px solid; border-top: 0.5px solid; background-color: #ffff0020; width: 100%; padding-top: 0.3em; padding-bottom: 0.3em"><font color="black"><div class="compact-block">
            <pre class="verbatim" xml:space="preserve">
%tikz
\begin{tikzpicture}
\draw [fill=red,ultra thick] (0,0) rectangle (1,1);
\draw [fill=red,ultra thick,red] (2,0) rectangle (3,1);
\draw [blue, fill=blue] (4,0) -- (5,1) -- (4.75,0.15) -- (4,0);
\draw [fill] (7,0.5) circle [radius=0.1];
\draw [fill=orange] (9,0) rectangle (11,1);
\draw [fill=white] (9.25,0.25) rectangle (10,1.5);
\end{tikzpicture}</pre>
          </div></font></td>
        </tr></tbody>
      </table></font>
    </p>
    <p>
      Press <font class="tmweb-key">&#x21E7;Return</font> to compile and display again.
      This process can be repeated as many times as one wishes.
    </p>
    <p>
      <em>Second function: get the inactivated image macro</em>
    </p>
    <p>
      Again with the cursor at the figure (in this case only after the figure:
      placing it before the figure does not make the function we are
      describing available), press <font class="tmweb-key">&#x232B;</font> and obtain the
      inactivated TeXmacs image macro:
    </p>
    <p>
      <font color="blue">&lt;</font>image<font color="blue">|</font><font color="#228"><tt></tt></font><font
      color="blue">|</font><font color="#288">0.618par</font><font color="blue">|</font><font
      color="#288"></font><font color="blue">|</font><font color="#288"></font><font color="blue">|</font><font
      color="#288"></font><font color="blue">&gt;</font>
    </p>
    <p>
      The script is now invisible (it is inside a hidden <font color="blue">&lt;</font>script-output<font
      color="blue">|</font><font color="#C68"></font><font color="blue">|</font><font color="#C68"></font><font
      color="blue">|</font><font color="#C68"></font><font color="blue">|</font><font color="#C68"></font><font
      color="blue">&gt;</font> macro).
    </p>
    <p>
      One possibility here is to adjust the width of the image: as an example,
      place the cursor in the field with the width and change it from 0.618par
      to 0.9par
    </p>
    <p>
      <font color="blue">&lt;</font>image<font color="blue">|</font><font color="#228"><tt></tt></font><font
      color="blue">|</font><font color="#288">0.9par</font><font color="blue">|</font><font
      color="#288"></font><font color="blue">|</font><font color="#288"></font><font color="blue">|</font><font
      color="#288"></font><font color="blue">&gt;</font>
    </p>
    <p>
      press <font class="tmweb-key">Return</font> to get back to the figure, which has
      now become larger:
    </p>
    <p>
      <img src="embedding-tikz-figures-short-6.png" style="margin-left: -0.0206868686868687em; margin-bottom: -0.0206868686868687em; margin-right: -0.020686868686866em; margin-top: 0em; vertical-align: 0em; height: 4.65753535353535em"></img>
    </p>
    <p>
      The initial script is again accessible (with the <font class="tmweb-key">Return</font>
      key), but accessing and executing it resets the figure size to 0.618par.
    </p>
    <p>
      The other parameter fields in the figure macro can be used to alter the
      height of the image (second parameter, in this case the image
      proportions are not preserved) and its placement with respect to the
      insertion point (third and fourth parameters):
    </p>
    <p>
      <font color="blue">&lt;</font>image<font color="blue">|</font><font color="#228"><tt></tt></font><font
      color="blue">|</font><font color="#288">0.5par</font><font color="blue">|</font><font
      color="#288"></font><font color="blue">|</font><font color="#288">0.2par</font><font
      color="blue">|</font><font color="#288">0.1par</font><font color="blue">&gt;</font>
    </p>
    <p>
      and again activating with <font class="tmweb-key">Return</font> obtain
    </p>
    <p>
      <img src="embedding-tikz-figures-short-8.png" style="margin-left: -0.0206868686868686em; margin-bottom: -0.0206868686868687em; margin-right: -0.020686868686866em; margin-top: 0em; vertical-align: 0em; height: 6.26165656565657em"></img>
    </p>
    <p>
      The white space above the line you are reading now is part of the image
      field, where the image itself has been sized down and shifted right and
      up by the change of the macro parameters.
    </p>
    <h2 id="auto-7">More complex pictures<span style="margin-left: 1em"></span></h2>
    <p>
      As in <class style="font-variant: small-caps">Graph</class> plugin sessions, one can place in
      <class style="font-variant: small-caps">fold</class> executables complex Ti<em>k</em>Z programs
      that depend on other LaTeX packages. In this case one should insert in
      the executable field the whole LaTeX document (the preferred class is
      <tt class="verbatim">standalone</tt>); the first line in this case must consist of
      the characters <tt class="verbatim">%pdflatex</tt> .
    </p>
    <p>
      Here is the same code (copied from this <a href="https://tex.stackexchange.com/questions/164155/2d-and-3d-vectors-in-tikz">TeX StackExchange
      question</a>) as in <a href="./a-tikz-example.html">Example of TikZ figure generation with the
      Graph plugin</a>; it needs the package <code>tikz-3dplot</code>:
    </p>
    <p>
      <font color="grey"><table style="display: inline-table; vertical-align: middle">
        <tbody><tr>
          <td style="border-left: 0.5px solid; border-right: 0.5px solid; border-bottom: 0.5px solid; border-top: 0.5px solid; background-color: #8080A0; padding-bottom: 0.15em; padding-top: 0.15em; text-align: center"><font style="font-size: 90.0%"><class style="font-family: sans-serif"><font color="white">Graph</font></class></font></td>
        </tr><tr>
          <td style="border-left: 0.5px solid; border-right: 0.5px solid; border-bottom: 0.5px solid; border-top: 0.5px solid; background-color: #ffff0020; width: 100%; padding-top: 0.3em; padding-bottom: 0.3em"><font color="black"><div class="compact-block">
            <pre class="verbatim" xml:space="preserve">
%pdflatex
\documentclass[tikz]{standalone}
\usepackage{tikz-3dplot}

\begin{document}

\tdplotsetmaincoords{60}{120}

\begin{tikzpicture}[
scale=3,
tdplot_main_coords,
axis/.style={-&gt;,blue,thick},
vector/.style={-stealth,red,very thick},
vector guide/.style={dashed,red,thick}]

%standard tikz coordinate definition using x, y, z coords
\coordinate (O) at (0,0,0);

%tikz-3dplot coordinate definition using r, theta, phi coords
\tdplotsetcoord{P}{.8}{55}{60}

%draw axes
\draw[axis] (0,0,0) -- (1,0,0) node[anchor=north east]{$x$};
\draw[axis] (0,0,0) -- (0,1,0) node[anchor=north west]{$y$};
\draw[axis] (0,0,0) -- (0,0,1) node[anchor=south]{$z$};

%draw a vector from O to P
\draw[vector] (O) -- (P);

%draw guide lines to components
\draw[vector guide] (O) -- (Pxy);
\draw[vector guide] (Pxy) -- (P);

\end{tikzpicture}
\end{document}</pre>
          </div></font></td>
        </tr></tbody>
      </table></font>
    </p>
    <p>
      Executing the code (press <font class="tmweb-key">&#x21E7;Return</font>) yields
    </p>
    <p>
      <img src="embedding-tikz-figures-short-10.png" style="margin-left: -0.0206868686868687em; margin-bottom: -0.0206868686868687em; margin-right: -0.0206868686868695em; margin-top: 0em; vertical-align: 0em; height: 21.3852525252525em"></img>
    </p>
    <p>
      Once more, the <class style="font-variant: small-caps">Fold</class> environment with the LaTeX
      code can be placed anywhere in a TeXmacs document, making it possible
      for example to place complex Ti<em>k</em>Z pictures inside TeXmacs image
      environments seamlessly.
    </p>
    <h2 id="auto-8">Error messages and troubleshooting<span style="margin-left: 1em"></span></h2>
    <ul>
      <li>
        <p>
          If the first line does not correspond to any of the backends known
          to <class style="font-variant: small-caps">Graph</class>, compilation will display a notice
          (if for example the first line is <tt class="verbatim">%pdftex</tt>, <class
          style="font-variant: small-caps">Graph</class> will answer: <tt class="verbatim">No such Graphs backend:
          pdftex</tt>). In this case substitute in the first line one of the
          recognized backends.
        </p>
      </li>
      <li>
        <p>
          The plugin may hang during compilation, displaying the notice <font
          color="red">Busy<span style="margin-left: 0.16665em"></span>.<span style="margin-left: 0.16665em"></span>.<span style="margin-left: 0.16665em"></span>.<span
          style="margin-left: 0.16665em"></span></font> in the executable <class style="font-variant: small-caps">fold</class>
          window; one case in which this may happen is if one has forgotten to
          end the LaTeX code with the command <tt class="verbatim">\end{document}</tt>.
        </p>
        <p>
          If this happens, none of the <class style="font-variant: small-caps">Graph</class> executable
          environments will compile anymore (even the ones that have already
          compiled once: all of them, if deactivated and recompiled, will
          display the <font color="red">Busy<span style="margin-left: 0.16665em"></span>.<span style="margin-left: 0.16665em"></span>.<span
          style="margin-left: 0.16665em"></span>.<span style="margin-left: 0.16665em"></span></font> notice without completing
          compilation). In this case one needs to completely close and restart
          TeXmacs.
        </p>
      </li>
    </ul>
  </body>
</html>